package com.eagleyng.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter{
	
	private int[] mTo;
	private String[] mFrom;
	private boolean mHaveMore = true;

	private ArrayList<HashMap<String, Object>> mData;
	private int mResource;
	private LayoutInflater mInflater;
	private OnItemClickListener[] mOnItemClickListener;
	private Context mContext;
	
	public ListAdapter(Context context, ArrayList<HashMap<String, Object>> data,
			int resource, String[] from, int[] to,OnItemClickListener[] onItemClickListener) {
		mContext = context;
		mResource = resource;
		mFrom = from;
		mTo = to;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mOnItemClickListener = onItemClickListener;
		setData(data);
	}
	
	/**
	 * @see android.widget.Adapter#getView(int, View, ViewGroup)
	 */
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(mData != null && mData.size()> 0){
			
			if(convertView == null) 
				convertView = mInflater.inflate(mResource, parent, false);
			
			final HashMap<String, Object>  dataSet = mData.get(position);
			
			int length = mTo.length;
			for(int i=0;i<length;i++){
				String key = mFrom[i];
				
				Object value = null;
				if(dataSet.containsKey(key)){
					value = dataSet.get(key);
				}
				
				View v = convertView.findViewById(mTo[i]);
				if (v instanceof TextView) {
					((TextView)v).setText(value==null?"":(String)value);
				}
				if (v instanceof RatingBar) {
					((RatingBar)v).setProgress(value==null?0:(Integer)value);
				}
				
				if(mOnItemClickListener != null){
					final OnItemClickListener lis = mOnItemClickListener[i];
					if(lis != null){
						v.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								lis.onItemClick(null, v, position, v.getId());
							}
						});
					}
				}
				
			}
			
		}
		return convertView;
	}
	
	public ArrayList<HashMap<String, Object>> getmData() {
		return mData;
	}
	
	public void setData(ArrayList<HashMap<String, Object>> data){
		mData = data;
		
		notifyDataSetChanged();
	}
	
	public boolean ismHaveMore() {
		return mHaveMore;
	}

	public void setmHaveMore(boolean mHaveMore) {
		this.mHaveMore = mHaveMore;
	}

	public String[] getmFrom() {
		return mFrom;
	}

	public void setmFrom(String[] from) {
		this.mFrom = from;
	}

	/**
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		return mData == null ? 0 : mData.size();
	}

	/**
	 * @see android.widget.Adapter#getItem(int)
	 */
	public Object getItem(int position) {
		return mData == null ? null : mData.get(position);
	}

	/**
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return position;
	}
	
}
